<?php
class ResourcesAction extends Action{
	/*
	*学习资源列表页
	*/
	public function ResourcesList(){
		$Categoryid = !$this->_get('category')?1:($this->_get('category'));
		
		import("ORG.Util.Page");// 导入分页类
		$count = M('studyresources')->where("Category={$Categoryid}")->count();
		$Page = new Page($count,15);
		$show = $Page->show();// 分页显示输出
		$Resources = M()->query("select s.Title,s.Id,s.HasFiles,s.CreateDate,u.TrueName from users u,studyresources s where s.CreateUser = u.UserId and Category={$Categoryid}  order by s.Id desc limit {$Page->firstRow},{$Page->listRows}");
		$this->assign('Resources',$Resources); 
		
		$Categorys = M('Category')->select();
		$this->assign('Categorys',$Categorys);
		
		$this->assign('page',$show);// 赋值分页输出

		$this->display();
	}
	/*
	*学习资源详情页
	*/
	public function Resources(){
		$id = $this->_get(id);
		$content = M('studyresources')->where("Id={$id}")->find();

		$org = M('Users')->where("UserId = {$content['CreateUser']}")->find();
		//dump($org);
		$this->assign('SchoolName',$org['OrgName']);
		$this->assign('TrueName',$org['TrueName']);
		
		
		//查询附件
		$attachlist = M('Studyresourceattachfiles')->where("ResourceId={$content['Id']}")->select();

		$attach = M()->query("select Id,FileName from files where Id in ({$attachlist[0]['FileId']}) ");//获取该学习资源包含的全部附件的在files表中的Id

		$this->assign('attach',$attach);
		
		$Category = $content['Category'];
		$this->assign('content',$content);
		
		//右边相关文章
		$article = M('studyresources')->where("Category={$Category} and Id !={$id}")->limit(10)->order('Id desc')->select();
		$this->assign('article',$article);
		
		M()->query("UPDATE studyresources SET ViewCount = ViewCount+1 WHERE Id={$id}");
		
		$this->display();
	}
	
	//学习资源下载
	public function downLoad(){
		$file = M('files')->where('Id='.$this->_get('id'))->find();
		//告诉浏览器字符集
		header('content-type:charset=utf-8');
		
		//告诉浏览器你的文件描述
		header('Content-Disposition:attachment;filename="'.$file[FileName].'"');
		//告诉浏览器文件的大小
		header('content-length:filesize(UpFile/Doc/StudyResources/'.$file[FileName].')');
		//显示内容
		readfile("UpFile/Doc/StudyResources/{$file['FileName']}");
	}
	
	//添加学习资源页面
	public function AddResources(){
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$id=$this->_get('id');
		if($id){
			$m = M('studyresources')->where("CreateUser={$uid} AND id={$id}")->find();
			$m['tag']=1;
			$this->assign('info',$m);
		}
		$category = M('category')->query("SELECT Id,CategoryName FROM __TABLE__");
		$this->assign('category',$category);
		$this->display();
	}
	
	//添加学习资源方法
	public function AddStudyResources(){
		$study = D('Studyresources');
		$result = $study->create();
		if($this->_post('tag')){
			$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];		
			$m = M('studyresources');
			$id=$this->_post('resourcesid');
			$data['Title']=$this->_post('Title');
			$data['Content']=$this->_post('Content');
			$data['Category']=$this->_post('Category');
			$data['Source']=$this->_post('Source');
			$m->where("Id={$id}")->save($data);
			$this->success('编辑成功','ResourcesList');
		}else{
			if (!$result){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				exit($study->getError());
			}else{
				// 验证通过 可以进行其他数据操作
				$arr['UserName'] = $_SESSION[C('USER_AUTH_KEY')]['UserName'];
				$arr['UserId'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
				$uploaduser = $arr['UserId'];
				$truename = $_SESSION[C('USER_AUTH_KEY')]['TrueName'];
				$arr['Title'] = $this->_post('Title');
				$arr['Category'] = $this->_post('Category');
				$arr['Content'] = $this->_post('Content');
				$arr['Source'] = $this->_post('Source');
				$arr['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
				$arr['CreateDate'] = date("Y-m-d H:i:s");
				$arr['HasFiles'] = $this->_post('HasFiles') ? 1 : 0;
				//附件写入数据库
				if(!!$file = $this->_post('HasFiles')){
					$hasfile = M('files');
					//转换附件数据结构
					$file = explode(';',substr($file,0,-1));
					foreach($file as $v){
						$filelist[] = explode('|',$v);
					}
					foreach($filelist as $v){
						$hasfile->execute("INSERT INTO __TABLE__ (FileName,FileSize,CreateDate,UpLoadUser,TrueName) VALUES ('{$v[0]}','{$v[1]}',now(),'{$uploaduser}','{$truename}')");
						$fileid[] = mysql_insert_id();
					}
					$fileid = implode(",",$fileid);
				}
				$resourceId = add_study_resources($arr);
				M('studyresourceattachfiles')->execute("INSERT INTO __TABLE__ (ResourceId,FileId) VALUES ('{$resourceId}','{$fileid}')");
				$this->success('添加成功','ResourcesList');
			}
		}
	}
	
	//添加附件
	public function proupfile(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 2*1024*1024 ;// 设置附件上传大小
		$upload->allowExts  = array('rar','doc','txt','pdf','docx','jpg','png','gif','bmp');// 设置附件上传类型
		$upload->savePath =  './UpFile/Doc/StudyResources/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			echo "<script>window.parent.uploadCallBack(false,null);</script>";
			$this->error($upload->getErrorMsg());
			
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			echo "<script>window.parent.uploadCallBack('{$info[0]['savename']}','{$info[0]['size']}');</script>";
		}
	}
	

	
}
